﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace GeoWeb.Demo.DataContext
{
    /// <summary>
    /// DataContext演示DEMO页面
    /// add by zhangyu
    /// 2013.01.14
    /// </summary>
    public partial class Demo : System.Web.UI.Page
    {
        /// <summary>
        /// 全局操作类
        /// </summary>
        DemoDataDal demo = new DemoDataDal();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string sType = Request.QueryString["type"];
                switch (sType)
                {
                    case "delete":
                        DeleteByID(Request.QueryString["id"]);
                        break;
                    case "edit":
                        EditByID(Request.QueryString["id"]);
                        break;
                    default:
                        //绑定数据
                        BindGridByData();
                        break;
                }
            }
        }

        /// <summary>
        /// 提交按钮事件
        /// add by zy 
        /// 2013.01.14
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            DemoTable entity = new DemoTable();

            entity.Demo_Name = txt_DemoName.Text.Trim();

            entity.Demo_Info = txt_DemoInfo.Text.Trim();

            int rows = demo.Add(entity);

            string sMessage = "添加失败！";

            if (rows > 0)
            {
                sMessage = "添加成功！";
                BindGridByData();
            }

            ClientScript.RegisterStartupScript(this.GetType(), "show", "alert('" + sMessage + "');", true);
        }

        /// <summary>
        /// 绑定测试数据到列表中
        /// add by zhangyu
        /// 2013.01.14
        /// </summary>
        private void BindGridByData()
        {
            List<DemoTable> list = demo.Select();

            Gv_DemoData.DataSource = list;

            Gv_DemoData.DataBind();
        }

        /// <summary>
        /// 根据ID进行删除
        /// add by zhangyu
        /// 2013.01.14
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        private void DeleteByID(string ID)
        {
            int rows = 0;

            rows = demo.Delete(Convert.ToInt32(ID));

            string sMessage = "删除失败！";

            if (rows > 0)
            {
                sMessage = "删除成功！";
                BindGridByData();
            }

            ClientScript.RegisterStartupScript(this.GetType(), "show", "alert('" + sMessage + "');", true);
        }

        /// <summary>
        /// 根据ID进行做编辑的准备 取出单条记录
        /// add by zhangyu
        /// 2013.01.14
        /// </summary>
        /// <param name="ID"></param>
        private void EditByID(string ID)
        {
            DemoTable entity = demo.SelectByID(Convert.ToInt32(ID));

            if (entity != null)
            {
                hdf_DemoID.Value = entity.Demo_ID.ToString();
                txt_DemoName.Text = entity.Demo_Name;
                txt_DemoInfo.Text = entity.Demo_Info;                
            }
            BindGridByData();
        }

        /// <summary>
        /// 根据ID进行做编辑的操作
        /// add by zhangyu
        /// 2013.01.14
        /// </summary>
        /// <param name="entity"></param>
        private void EditByEntity(DemoTable entity)
        {
            int rows = 0;

            rows = demo.UpdateByEntity(entity);

            string sMessage = "修改失败！";

            if (rows > 0)
            {
                sMessage = "修改成功！";
                BindGridByData();
            }

            ClientScript.RegisterStartupScript(this.GetType(), "show", "alert('" + sMessage + "');", true);
        }

        /// <summary>
        /// 编辑按钮的点击事件
        /// add by zhangyu
        /// 2013.01.14
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_edit_Click(object sender, EventArgs e)
        {
            string sMessage = "请先选择要修改的记录！";
            if (string.IsNullOrEmpty(hdf_DemoID.Value))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "show", "alert('" + sMessage + "');", true);
            }
            else
            {
                sMessage = "修改失败！";
                DemoTable entity = new DemoTable();
                entity.Demo_ID = Convert.ToInt32(hdf_DemoID.Value);
                entity.Demo_Name = txt_DemoName.Text;
                entity.Demo_Info = txt_DemoInfo.Text;
                EditByEntity(entity);
            }
        }
    }
}